package GUI;

import java.awt.BorderLayout;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.border.BevelBorder;
import java.awt.Color;
import javax.swing.border.CompoundBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.TitledBorder;
import javax.swing.text.MaskFormatter;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import Classes.Aluno;
import Repositorio.Rep_Aluno;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.awt.Window.Type;

public class CadAluno extends JFrame {

	private JPanel contentPane;
	private static JTextField txtNome;
	private static JTextField txtCPF;
	private static JTextField txtAnoEnt;
	private static JTextField txtNomeMae;
	private static JTextField txtAnoSaida;
	private JTextField txtEndereco;


	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CadAluno frame = new CadAluno();
					frame.setVisible(true);
					frame.setLocationRelativeTo(null);
					LimpaCampos();
					txtNome.requestFocus();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException | UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			
		}
	}

	/**
	 * Create the frame.
	 * @throws ParseException 
	 */
	
	
	public CadAluno() throws ParseException {
		JOptionPane.setRootFrame(this);
		setResizable(false);
		setAlwaysOnTop(true);
		setResizable(false);
		setType(Type.NORMAL);
		
		setTitle("Cadastro de Aluno ");
		setBounds(100, 100, 622, 527);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(4, 5, 602, 499);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_1.setBounds(6, 11, 582, 59);
		panel.add(panel_1);
		panel_1.setLayout(null);
		
		JLabel lblCadastroDeUsurio = new JLabel("Cadastro de Aluno");
		lblCadastroDeUsurio.setFont(new Font("Tahoma", Font.PLAIN, 22));
		lblCadastroDeUsurio.setBounds(186, 15, 202, 33);
		panel_1.add(lblCadastroDeUsurio);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_2.setBounds(6, 81, 582, 361);
		panel.add(panel_2);
		panel_2.setLayout(null);
		
		
		txtNome = new JTextField();
		txtNome.setBounds(88, 35, 484, 20);
		panel_2.add(txtNome);
		txtNome.setColumns(10);
		
		
		MaskFormatter mskCpf = new MaskFormatter("###.###.###-##");
		txtCPF = new JFormattedTextField(mskCpf);
		txtCPF.setColumns(10);
		txtCPF.setBounds(88, 125, 210, 20);
		panel_2.add(txtCPF);
		
		MaskFormatter mskAno_Entrada = new MaskFormatter("####");
		txtAnoEnt = new JFormattedTextField(mskAno_Entrada);
		txtAnoEnt.setColumns(10);
		txtAnoEnt.setBounds(88, 292, 94, 20);
		panel_2.add(txtAnoEnt);
		
		txtNomeMae = new JTextField();
		txtNomeMae.setColumns(10);
		txtNomeMae.setBounds(88, 66, 484, 20);
		panel_2.add(txtNomeMae);
		
		MaskFormatter mskAno_Saida = new MaskFormatter("####");
		txtAnoSaida = new JFormattedTextField(mskAno_Saida);
		txtAnoSaida.setColumns(10);
		txtAnoSaida.setBounds(88, 323, 94, 20);
		panel_2.add(txtAnoSaida);
		
		JLabel lblNome = new JLabel("Nome");
		lblNome.setBounds(10, 38, 46, 14);
		panel_2.add(lblNome);
		
		JLabel lblNomeMe = new JLabel("Nome M\u00E3e");
		lblNomeMe.setBounds(10, 69, 68, 14);
		panel_2.add(lblNomeMe);
		
		JLabel lblCpf = new JLabel("CPF");
		lblCpf.setBounds(10, 128, 46, 14);
		panel_2.add(lblCpf);
		
		MaskFormatter mskanoin = new MaskFormatter("####");
		JLabel lblAnoEntrada = new JLabel("Ano Entrada");
		lblAnoEntrada.setBounds(10, 295, 68, 14);
		panel_2.add(lblAnoEntrada);
		
		JLabel lblAnoSada = new JLabel("Ano Sa\u00EDda");
		lblAnoSada.setBounds(10, 326, 68, 14);
		panel_2.add(lblAnoSada);
		
		final JComboBox cmbCurso = new JComboBox();
		cmbCurso.setModel(new DefaultComboBoxModel(new String[] {"Ciencias da Comp.", "Contabilidade", "Direito"}));
		cmbCurso.setBounds(88, 227, 210, 20);
		panel_2.add(cmbCurso);
		
		JLabel lblCurso = new JLabel("Curso");
		lblCurso.setBounds(10, 230, 46, 14);
		panel_2.add(lblCurso);
		
		JLabel lblSitAtual = new JLabel("Sit. Atual");
		lblSitAtual.setBounds(308, 260, 68, 14);
		panel_2.add(lblSitAtual);
		
		final JComboBox cmbSitAtual = new JComboBox();
		cmbSitAtual.setModel(new DefaultComboBoxModel(new String[] {"Cursando", "Trancado", "Formado", "Abandono"}));
		cmbSitAtual.setBounds(366, 257, 200, 20);
		panel_2.add(cmbSitAtual);
		
		JLabel lblStatusPag = new JLabel("Status Pag.");
		lblStatusPag.setBounds(10, 263, 68, 14);
		panel_2.add(lblStatusPag);
		
		final JComboBox cmbStatusPag = new JComboBox();
		cmbStatusPag.setModel(new DefaultComboBoxModel(new String[] {"Pagante", "Bolsista", "Desconto Mensalidade"}));
		cmbStatusPag.setBounds(88, 260, 210, 20);
		panel_2.add(cmbStatusPag);
		
		JLabel lblEndereco = new JLabel("Endereco");
		lblEndereco.setBounds(10, 100, 68, 14);
		panel_2.add(lblEndereco);
		
		txtEndereco = new JTextField();
		txtEndereco.setColumns(10);
		txtEndereco.setBounds(88, 97, 484, 20);
		panel_2.add(txtEndereco);
		
		JLabel lblCep = new JLabel("UF");
		lblCep.setBounds(10, 161, 46, 14);
		panel_2.add(lblCep);
		
		final JComboBox cmbUF = new JComboBox();
		cmbUF.setModel(new DefaultComboBoxModel(new String[] {"AL", "AM", "AP", "BA", "CE", "DF", "ES", "GO", "MA", "MG", "MS", "MT", "PA", "PB", "PE", "PI", "PR", "RJ", "RN", "RO", "RR", "RS", "SC", "SE", "SP", "TO"}));
		cmbUF.setBounds(88, 158, 46, 20);
		panel_2.add(cmbUF);
		
		JLabel lblEmail = new JLabel("e-mail");
		lblEmail.setBounds(10, 192, 68, 14);
		panel_2.add(lblEmail);
		
		
		final JTextField txtEmail = new JTextField();
		txtEmail.setColumns(10);
		txtEmail.setBounds(88, 189, 210, 20);
		panel_2.add(txtEmail);
		
		JLabel lblCep_1 = new JLabel("CEP");
		lblCep_1.setBounds(321, 128, 68, 14);
		panel_2.add(lblCep_1);
		
		MaskFormatter mskcep = new MaskFormatter("#####-###");
		final JFormattedTextField txtCEP = new JFormattedTextField(mskcep);
		txtCEP.setColumns(10);
		txtCEP.setBounds(383, 125, 189, 20);
		panel_2.add(txtCEP);
		
		JLabel lblTelefone = new JLabel("Telefone");
		lblTelefone.setBounds(321, 189, 68, 14);
		panel_2.add(lblTelefone);
		
		MaskFormatter msktel = new MaskFormatter("(##)####-####");
		final JFormattedTextField txtTelefone = new JFormattedTextField(msktel);
		txtTelefone.setColumns(10);
		txtTelefone.setBounds(383, 186, 189, 20);
		panel_2.add(txtTelefone);
		
		final JButton btnSalvar = new JButton("Salvar");
		//btnSalvar.setEnabled(false);

		btnSalvar.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) {
					
								
					Aluno aluno = new Aluno();
					Rep_Aluno rep = new Rep_Aluno();
 
					//List<Aluno> aluno_lista = new ArrayList();
					aluno.setNome(txtNome.getText());
					aluno.setNome_mae(txtNomeMae.getText());
					aluno.setCpf(txtCPF.getText());
					aluno.setCurso(cmbCurso.getSelectedItem().toString());	
					aluno.setAno_entrada(txtAnoEnt.getText());
					aluno.setAno_saida(txtAnoSaida.getText());
					aluno.setSit_atual(cmbSitAtual.getSelectedItem().toString());
					aluno.setStatus_pag(cmbStatusPag.getSelectedItem().toString());
					aluno.setCep(txtCEP.getText());
					aluno.setEmail(txtEmail.getText());
					aluno.setTelefone(txtTelefone.getText());
					aluno.setEndereco(txtEndereco.getText());
					aluno.setUf(cmbUF.getSelectedItem().toString());
					
					
					//Executa o cadastro no banco e em seguida limpa campos
					String verifica = rep.VerificaCpf(txtCPF.getText());
					if (verifica.equalsIgnoreCase(txtCPF.getText()))
					{
						JOptionPane.showMessageDialog(null, "CPF ja cadastrado no Banco, favor colocar outro.");
						LimpaCampos();
						txtNome.requestFocus();
					}
					else
						rep.InserirAlunoDB(aluno);
						LimpaCampos();
						txtNome.requestFocus();
			
				
			}
		});
		btnSalvar.setIcon(new ImageIcon(CadAluno.class.getResource("/javax/swing/plaf/metal/icons/ocean/floppy.gif")));
		btnSalvar.setBounds(6, 453, 136, 32);
		panel.add(btnSalvar);
		
		final JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{	
				dispose();				
			}
		});
		btnCancelar.setIcon(new ImageIcon(CadAluno.class.getResource("/javax/swing/plaf/metal/icons/ocean/error.png")));
		btnCancelar.setBounds(446, 453, 142, 32);
		panel.add(btnCancelar);
		
	}
	
	static void LimpaCampos()
	{
		txtNome.setText("");
		txtNomeMae.setText("");
		txtCPF.setText("");
		txtAnoEnt.setText("");
		txtAnoSaida.setText("");
	}
}	

	
	
